package timing.ukulele.third.service.common;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import timing.ukulele.persistence.model.BaseModel;
import timing.ukulele.persistence.service.BaseService;
import timing.ukulele.persistence.valide.Insert;
import timing.ukulele.persistence.valide.Update;
import timing.ukulele.web.model.OperatorInfo;
import timing.ukulele.web.util.RequestUtil;
import java.util.Collection;

public class BaseOperatorService<M extends BaseMapper<T>, T extends BaseModel<?>> extends BaseService<M, T> {

    public boolean save(@Validated(Insert.class)T entity) {
        OperatorInfo info = RequestUtil.getOperator();
        entity.setCreateBy(info.getUsername());
        entity.setUpdateBy(info.getUsername());
        return super.save(entity);
    }

    public boolean saveBatch(@Validated(Insert.class) Collection<T> entityList, int batchSize) {
        if (CollectionUtils.isEmpty(entityList)) {
            return false;
        } else {
            OperatorInfo info = RequestUtil.getOperator();
            entityList.forEach((entity) -> {
                entity.setCreateBy(info.getUsername());
                entity.setUpdateBy(info.getUsername());
            });
            return super.saveBatch(entityList, batchSize);
        }
    }

    public boolean saveOrUpdate(@Validated({Insert.class, Update.class})T entity) {
        OperatorInfo info = RequestUtil.getOperator();
        entity.setUpdateBy(info.getUsername());
        if (entity.getId() == null) {
            entity.setCreateBy(info.getUsername());
        } else {
            entity.setCreateBy(null);
        }
        return super.saveOrUpdate(entity);
    }

    public boolean saveOrUpdateBatch(@Validated({Insert.class, Update.class})Collection<T> entityList) {
        if (CollectionUtils.isEmpty(entityList)) {
            return false;
        } else {
            OperatorInfo info = RequestUtil.getOperator();
            entityList.forEach((entity) -> {
                entity.setUpdateBy(info.getUsername());
                if (entity.getId() == null) {
                    entity.setCreateBy(info.getUsername());
                    if (entity.getDeleted() == null) {
                        entity.setDeleted(Boolean.FALSE);
                    }
                } else {
                    entity.setCreateBy(null);
                }
            });
            return super.saveOrUpdateBatch(entityList);
        }
    }

    public boolean saveOrUpdateBatch(@Validated({Insert.class, Update.class})Collection<T> entityList, int batchSize) {
        if (CollectionUtils.isEmpty(entityList)) {
            return false;
        } else {
            OperatorInfo info = RequestUtil.getOperator();
            entityList.forEach((entity) -> {
                entity.setUpdateBy(info.getUsername());
                if (entity.getId() == null) {
                    entity.setCreateBy(info.getUsername());
                    if (entity.getDeleted() == null) {
                        entity.setDeleted(Boolean.FALSE);
                    }
                } else {
                    entity.setCreateBy(null);
                }
            });
            return super.saveOrUpdateBatch(entityList, batchSize);
        }
    }

    public boolean updateById(T entity) {
        OperatorInfo info = RequestUtil.getOperator();
        entity.setUpdateBy(info.getUsername());
        entity.setCreateBy(null);
        return super.updateById(entity);
    }

    public boolean update(T entity, Wrapper<T> updateWrapper) {
        OperatorInfo info = RequestUtil.getOperator();
        entity.setUpdateBy(info.getUsername());
        entity.setCreateBy(null);
        return super.update(entity, updateWrapper);
    }

    public boolean updateBatchById(Collection<T> entityList, int batchSize) {
        if (CollectionUtils.isEmpty(entityList)) {
            return false;
        } else {
            OperatorInfo authInfo = RequestUtil.getOperator();
            entityList.forEach((entity) -> {
                entity.setUpdateBy(authInfo.getUsername());
                entity.setCreateBy(null);
            });
            return super.updateBatchById(entityList, batchSize);
        }
    }
}
